package kr.kwakyc.puzzle;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimerPanel extends JPanel {
	
	private JLabel lblTimer;
	private int minute;
	private int second;
	
	private Timer timer;
	
	public TimerPanel(){
//		this.setPreferredSize(new Dimension(100, 100));
		
		JLabel lblTimerText = new JLabel("진행 시간"); 
		lblTimer = new JLabel();
		
		this.add(lblTimerText);
		this.add(lblTimer);
		
		updateTimer();
		
		timer = new Timer();
		timer.scheduleAtFixedRate(new MyTimerTask(this), new Date(), 1 * 1000);
	}
	
	private void updateTimer() {
		lblTimer.setText(String.format("%02d:%02d", minute, second));
	}

	public void addSecond(){
		second = second + 1;
		if(second == 60){
			minute = minute + 1;
			second = 0;
		}
	}
	
	public void finish(){
		timer.cancel();
	}
	
	public int getSecond(){
		return (minute * 60) + second;
	}
	
	class MyTimerTask extends TimerTask{
		private TimerPanel timerPanel;
		
		public MyTimerTask(TimerPanel timerPanel){
			this.timerPanel = timerPanel;
		}

		@Override
		public void run() {
			timerPanel.addSecond();
			timerPanel.updateTimer();
		}
	}
}
